let fetch = require("../../utils/fetch");
let app = getApp();
Page({
	data: {
		phoneNumber: '', //手机号
		identifyCode: '', //用户输入的验证码
		countdownText: "获取验证码",
		countdown: 60, //倒计时的时长
		isSending: false, //是否点击发送了验证码，用于阻止多次点击
		showTips: false, //绑定手机号异常的提示开关
		isRightPhone: false, //手机号验证开关
		canSend: false, //验证码是否输入的开关
		imageUrl: app.globalData.imageUrl,
		imgList: ['bannerv3-1.png', 'bannerv3-2.png', 'bannerv3-3.png'],

	},
	onLoad: function(options) {},
	bindPhoneInput: function(e) {
		this.setData({
			phoneNumber: e.detail.value
		})
		if (this.regPhone(e.detail.value)) {
			this.setData({
				isRightPhone: true
			})
		} else {
			this.setData({
				isRightPhone: false
			})

		}
	},
	bindCodeInput: function(e) {
		this.setData({
			identifyCode: e.detail.value
		})
		if (this.regPhone() && this.data.identifyCode) {
			this.setData({
				canSend: true
			})
		} else {
			this.setData({
				canSend: false
			})

		}

	},
	regPhone: function() {
		const myreg = /^1(3|4|5|7|8)\d{9}$/;
		const phoneNumber = this.data.phoneNumber;
		return myreg.test(phoneNumber)
	},
	sendCode: function() {
		const flag = this.regPhone();
		if (!flag) {
			wx.showToast({
				title: '请检查手机号',
				image: "../../image/warn.png",
				duration: 1000
			})
			return;
		}
		fetch.acquireCode(this.data.phoneNumber).then((response) => {
			const res = response.data;
			if (res.code == 200) {
				this.countdown()
				wx.showToast({
					title: '发送成功',
					icon: 'success',
					duration: 2000
				})
			}
		}).catch((err) => {
			fetch.showMyToast(this, err.data.des)
		})

	},
	bindPhone: function() {
		if (this.regPhone() && this.data.identifyCode) {
			const openId = wx.getStorageSync("openId"),
				shopCode = app.globalData.common.shopCode,
				userInfo = app.globalData.userInfo,
				mobile = this.data.phoneNumber,
				vc = this.data.identifyCode;
			console.log(userInfo)
			fetch.loadingText()
			fetch.bindPhone(openId, 6, shopCode, userInfo, mobile, vc).then((response) => {
				fetch.hideLoadingText()
				const res = response.data;
				if (res.code == 200) {
					wx.setStorageSync('newUser', false);
					app.globalData.header.Cookie = 'SESSION=' + res.obj.session;
					app.globalData.userInfo = Object.assign(app.globalData.userInfo, res.obj)
					wx.showToast({
						title: '注册成功',
						icon: 'success',
						duration: 2000
					})
					wx.switchTab({
						url: '../index/index'
					})

				}
			}).catch((err) => {
				fetch.hideLoadingText()
				console.log(err)
				fetch.showMyToast(this, err.data.des)
			})
		} else {
			if (!this.regPhone()) {
				wx.showToast({
					title: '手机号有误',
					image: "../../image/warn.png",
					duration: 1000
				})
				return
			}
			if (!this.data.identifyCode) {
				wx.showToast({
					title: '验证码有误',
					image: "../../image/warn.png",
					duration: 1000
				})
				return
			}
		}
	},
	countdown: function() {
		let t = null;
		clearInterval(t)
		t = setInterval(() => {
			if (this.data.countdown > 0) {
				// console.log('countdown', this.data.countdown)
				this.setData({
					countdown: this.data.countdown - 1,
					countdownText: (this.data.countdown) + "s",
					isSending: true
				})
			} else {
				this.setData({
					countdown: 60,
					countdownText: "获取验证码",
					isSending: false
				})
				clearInterval(t)
				return;
			}
		}, 1000);
	},

	onReady: function() {
		console.log('onReady')
	},
	onShow: function() {

	},
	onHide: function() {},
	onUnload: function() {
		console.log('onUnload')
	},

	checkNetwork: function() {
		fetch.NetworkType().then((res) => {
			console.log(res);
			const networkType = res.networkType;
			console.log(networkType)
			if (networkType == '2g' || networkType == 'none' || networkType == 'unknown') {
				const self = this;
				fetch.showMyToast(self, `当前网络状况为：${networkType}，可能会影响使用效果`)
			}
		})
	}
})